<template>
    <div class="login-view">
        <div class="lottieContainer-view">
            <video src="@/assets/mp4/background.mp4" autoplay muted loop></video>
        </div>
        <div class="login-container">
            <div class="login-container-view">
                <div class="welcode-title">欢迎使用！</div>
                <!-- 新增登录模式切换 -->
                <div class="mode-toggle">
                    <el-button :class="{ 'active-mode': loginMode === 'password' }"
                        @click="loginMode = 'password'">账号密码登录</el-button>
                    <el-button :class="{ 'active-mode': loginMode === 'sms' }"
                        @click="loginMode = 'sms'">手机验证码登录</el-button>
                </div>

                <!-- 密码登录表单 -->
                <div v-if="loginMode === 'password'">
                    <div class="form-item">
                        <el-input 
                            v-model="username" 
                            placeholder="请输入账号"
                            :class="['login-input', { 'input-active': username }]">
                            <template #prefix>
                                <span class="input-icon">👤</span>
                            </template>
                        </el-input>
                    </div>
                    <div class="form-item">
                        <el-input 
                            v-model="password" 
                            placeholder="请输入密码"
                            :class="['login-input', { 'input-active': password }]">
                            <template #prefix>
                                <span class="input-icon">🔑</span>
                            </template>
                        </el-input>
                    </div>
                </div>

                <!-- 原有短信登录表单 -->
                <div v-else>
                    <div class="form-item">
                        <el-input v-model="phone" placeholder="请输入手机号"
                            :class="['login-input', { 'input-active': phone }]">
                            <template #prefix>
                                <span class="input-icon">📱</span>
                            </template>
                        </el-input>
                    </div>

                    <div class="form-item code-item">
                        <el-input v-model="code" placeholder="请输入验证码"
                            :class="['login-input', { 'input-active': code }]">
                            <template #prefix>
                                <span class="input-icon">🔒</span>
                            </template>
                        </el-input>
                        <el-button class="get-code-button" :disabled="countdown > 0" @click="getSMSCode">
                            {{ countdown ? `${countdown}s` : '获取验证码' }}
                        </el-button>
                    </div>
                </div>

                <el-button class="login-register-button" @click="loginIn">
                    {{ loginMode === 'password' ? '立即登录' : '登录/注册' }}
                </el-button>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/apis'
import { ElMessage } from 'element-plus' // 显式导入消息组件

const router = useRouter()
const phone = ref('')
const code = ref('')
const countdown = ref(0)
let timer = null // 保存定时器引用
const clearStrong = () => {
    localStorage.removeItem('token')
    localStorage.removeItem('expiresIn')
}

// 清除定时器
onUnmounted(() => {
    if (timer) clearInterval(timer)
})
onMounted(() => {
    clearStrong()
})

// 新增响应式数据
const loginMode = ref('sms') // 登录模式：sms/password
const username = ref('')
const password = ref('')

// 修改登录方法
const loginIn = () => {
    if (loginMode.value === 'sms') {
        // 原有短信登录验证
        if (!phone.value || !code.value) {
            ElMessage.error('请填写完整信息')
            return
        }
        handleSMSLogin()
    } else {
        // 密码登录验证
        if (!username.value || !password.value) {
            ElMessage.error('请输入账号和密码')
            return
        }
        handlePasswordLogin()
    }
}

const getSMSCode = () => {
    if (!phone.value) {
        ElMessage.error('请输入手机号')
        return
    }
    api.auth.getCode({
        phone: phone.value
    }).then(res => {
        if (res.code === 200) {
            ElMessage.success('验证码发送成功')
            startCountdown()
        }
    }).catch(() => {
        ElMessage.error('请求失败，请检查网络')
    })
}

// 新增密码登录处理
const handlePasswordLogin = () => {
    api.auth.loginIn({
        username: username.value,
        password: password.value
    }).then(res => {
        if (res.code === 200) {
            setToken(res.token)
            ElMessage.success('登录成功')
            router.replace('/')
        }
    }).catch(handleLoginError)
}

const setToken = (token) => {
    localStorage.setItem('token', token)
    localStorage.setItem('expiresIn', Date.now() + 3600000)
}

// 原有短信登录处理改名
const handleSMSLogin = () => {
    api.auth.verifyCode({
        phone: phone.value,
        code: code.value
    }).then(res => {
        if (res.code === 200) {
            setToken(res.token)
            ElMessage.success('登录成功')
            router.replace('/') // 改用replace避免回退到登录页
        } else {
            ElMessage.error(res.message || '登录失败')
        }
    }).catch(() => {
        ElMessage.error('请求失败，请检查网络')
    })
}

// 新增统一错误处理
const handleLoginError = (error) => {
    ElMessage.error(error.message || '登录失败，请检查凭证')
}
</script>

<style scoped>
.login-view {
    display: flex;
    height: 100vh;
    width: 100%;
    background: #ffffff;
}

.lottieContainer-view {
    flex: 0 0 500px;
    height: 100%;
    width: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-container {
    flex: 1;
    height: 100%;
    position: relative;
}

.login-container-view {
    font-size: 16px;
    border-radius: 24px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.welcode-title {
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    color: #333333;
    margin-bottom: 30px;
}

.login-register-button {
    border-radius: 30px;
    width: 360px;
    height: 60px;
    line-height: 60px;
    background: #2878ff;
    color: #ffffff;
    border: none;
    transition: all 0.3s ease;
}

.login-register-button:hover {
    background: #5191ff;
    color: #eeeeee;
    box-shadow: 0 0 10px #5191ff;
}

/* 新增样式 */
.form-item {
    margin-bottom: 30px;
    width: 360px;
}

.code-item {
    display: flex;
    gap: 10px;
}

.login-input {
    width: 100%;
    height: 60px;
}

.login-input :deep(.el-input__wrapper) {
    border-radius: 30px !important;
    padding: 0 25px;
    box-shadow: 0 0 0 1px #e0e0e0 inset !important;
    transition: all 0.3s ease;
}

.input-active :deep(.el-input__wrapper) {
    box-shadow: 0 0 0 2px #2878ff inset !important;
}

.input-icon {
    font-size: 20px;
    margin-right: 10px;
}

.get-code-button {
    flex-shrink: 0;
    width: 140px;
    height: 60px;
    border-radius: 30px;
    background: #2878ff;
    /* 修改背景色 */
    color: white;
    /* 修改文字颜色 */
    border: none;
    font-size: 14px;
    transition: all 0.3s ease;
}

.get-code-button:hover {
    background: #5191ff;
    color: #eeeeee;
    box-shadow: 0 0 10px #5191ff;
}

.get-code-button.is-disabled {
    background: #a0c0ff;
    /* 调整禁用状态为浅蓝色 */
    color: rgba(255, 255, 255, 0.8);
    /* 保持白色但降低透明度 */
}

.mode-toggle {
    margin-bottom: 30px;
    display: flex;
    gap: 20px;
    justify-content: center;
}

.mode-toggle .el-button {
    border: none;
    background: transparent;
    color: #666;
}

.mode-toggle .active-mode {
    color: #2878ff;
    font-weight: bold;
    border-bottom: 2px solid #2878ff;
    border-radius: 0;
}
</style>